/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import javax.swing.JOptionPane;
import socof.wget.SOCOFWget;

/**
 *
 * @author Gilberto Pereira <1040942@isep.ipp.pt>
 */
public class UserInterface extends javax.swing.JPanel {

    private String jTextField1TXT;
    private static UserInterface gr = new UserInterface();

    public static UserInterface getInstance() {
        return UserInterface.gr;
    }

    /**
     * Creates new form Inicial
     */
    public UserInterface() {
        
        
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        jToggleButton1 = new javax.swing.JToggleButton();
        jTextXXX = new javax.swing.JSplitPane();
        urlTxt = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        localPathTxt = new javax.swing.JTextField();
        startButton = new javax.swing.JButton();
        pauseButton = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        maxBandwithTxt = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        maxCpuTxt = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        maxDownloaders = new javax.swing.JTextField();
        maxParsers = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        urlsParsed = new javax.swing.JTextPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        urlsDownloaded = new javax.swing.JTextPane();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        numberDownloadersTxt = new javax.swing.JTextField();
        numberParsersTxt = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        maxLevel = new javax.swing.JTextField();
        resumeButton = new javax.swing.JButton();

        jToggleButton1.setText("jToggleButton1");

        urlTxt.setText("tek.sapo.pt");
        urlTxt.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                urlTxtActionPerformed(evt);
            }
        });
        urlTxt.addInputMethodListener(new java.awt.event.InputMethodListener()
        {
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt)
            {
                urlTxtInputMethodTextChanged(evt);
            }
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt)
            {
            }
        });

        jLabel1.setText("Url:");

        jLabel2.setText("Local a guardar:");

        localPathTxt.setText("C:\\Users\\Carlos Pinheiro\\Downloads\\wget");
        localPathTxt.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                localPathTxtActionPerformed(evt);
            }
        });

        startButton.setText("Iniciar");
        startButton.setToolTipText("");
        startButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                startActionPerformed(evt);
            }
        });

        pauseButton.setText("Pausa");
        pauseButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                pauseActionPerformed(evt);
            }
        });

        exitButton.setText("Sair");
        exitButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                exitActionPerformed(evt);
            }
        });

        jLabel3.setText("Máx. largura de banda (max kb):");

        maxBandwithTxt.setText("100000");
        maxBandwithTxt.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                maxBandwithTxtActionPerformed(evt);
            }
        });

        jLabel4.setText("Máx. carga de cpu:");

        maxCpuTxt.setText("0.25");

        jLabel5.setText("Máx. número de downloaders:");

        jLabel6.setText("Máx número de parsers:");

        maxDownloaders.setText("10");

        maxParsers.setText("5");

        urlsParsed.setEditable(false);
        urlsParsed.addPropertyChangeListener(new java.beans.PropertyChangeListener()
        {
            public void propertyChange(java.beans.PropertyChangeEvent evt)
            {
                urlsParsedPropertyChange(evt);
            }
        });
        jScrollPane1.setViewportView(urlsParsed);
        urlsParsed.getAccessibleContext().setAccessibleParent(urlsParsed);

        urlsDownloaded.setEditable(false);
        urlsDownloaded.addPropertyChangeListener(new java.beans.PropertyChangeListener()
        {
            public void propertyChange(java.beans.PropertyChangeEvent evt)
            {
                urlsDownloadedPropertyChange(evt);
            }
        });
        jScrollPane2.setViewportView(urlsDownloaded);

        jLabel7.setText("Processos de download");

        jLabel8.setText("Processos de parse");

        numberDownloadersTxt.setText("0");

        numberParsersTxt.setText("0");
        numberParsersTxt.setToolTipText("");

        jLabel9.setText("Nível máximo:");

        maxLevel.setText("3");

        resumeButton.setText("Continuar");
        resumeButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                resumeActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(16, 16, 16)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel4)
                                    .add(jLabel6)
                                    .add(jLabel5)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                            .add(jLabel9)
                                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .add(maxLevel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 92, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                            .add(maxParsers, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 92, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                            .add(layout.createSequentialGroup()
                                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                                    .add(jLabel2)
                                                    .add(jLabel1))
                                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                                    .add(urlTxt, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
                                                    .add(localPathTxt)
                                                    .add(org.jdesktop.layout.GroupLayout.TRAILING, maxBandwithTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 92, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                    .add(org.jdesktop.layout.GroupLayout.TRAILING, maxDownloaders, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 92, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                    .add(org.jdesktop.layout.GroupLayout.TRAILING, maxCpuTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 92, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))))
                                .add(12, 12, 12))))
                    .add(layout.createSequentialGroup()
                        .add(77, 77, 77)
                        .add(startButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
                        .add(144, 144, 144)
                        .add(pauseButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(layout.createSequentialGroup()
                                .add(jLabel7)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(numberDownloadersTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(layout.createSequentialGroup()
                                .add(jLabel8)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(numberParsersTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE)
                            .add(jScrollPane1))
                        .addContainerGap(29, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(160, 160, 160)
                        .add(resumeButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                        .add(132, 132, 132)
                        .add(exitButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(134, 134, 134))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(23, 23, 23)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel1)
                            .add(urlTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel7)
                            .add(numberDownloadersTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 106, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 106, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(0, 0, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(65, 65, 65)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel2)
                            .add(localPathTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel3)
                            .add(maxBandwithTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel4)
                            .add(maxCpuTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel5)
                            .add(maxDownloaders, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel6)
                            .add(maxParsers, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel8)
                            .add(numberParsersTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(18, 18, 18)
                                .add(jLabel9)
                                .add(137, 137, 137)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                    .add(pauseButton)
                                    .add(exitButton)
                                    .add(startButton)
                                    .add(resumeButton)))
                            .add(layout.createSequentialGroup()
                                .add(14, 14, 14)
                                .add(maxLevel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
                .add(29, 29, 29))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void urlTxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_urlTxtActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_urlTxtActionPerformed

    private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
        // TODO add your handling code here:
       SOCOFWget.finish();
       stopMonitorProcess();
       System.exit(WIDTH);
    }//GEN-LAST:event_exitActionPerformed

    private void urlTxtInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_urlTxtInputMethodTextChanged
        // TODO add your handling code here:
        //    this.jTextField1TXT = urlTxt.getText();
    }//GEN-LAST:event_urlTxtInputMethodTextChanged

    private void startActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startActionPerformed
        // TODO add your handling code here:
        //classExterna.start(urlTxt.getText(), localPathTxt.getText(), maxBandwithTxt.getText(), maxCpuTxt.getText(), maxDownloaders.getText(), maxParsers.getText());
        boolean ok = SOCOFWget.setValues(urlTxt.getText(), localPathTxt.getText(), maxBandwithTxt.getText(),
                                        maxCpuTxt.getText(), maxDownloaders.getText(), maxParsers.getText(),
                                        maxLevel.getText());
        if (!ok)
        {
            JOptionPane.showMessageDialog(this, "P.f. introduza os valore obrigatórios.");
        }
        else
        {
            SOCOFWget.startProcess();
            startMonitorProcess();
            
        }
        //   classExterna.start("vamos lá iniciar a coisa");

    }//GEN-LAST:event_startActionPerformed

    private void pauseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pauseActionPerformed
        SOCOFWget.pause();
    }//GEN-LAST:event_pauseActionPerformed

    private void localPathTxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_localPathTxtActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_localPathTxtActionPerformed

    private void maxBandwithTxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_maxBandwithTxtActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_maxBandwithTxtActionPerformed

    private void resumeActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_resumeActionPerformed
    {//GEN-HEADEREND:event_resumeActionPerformed
        SOCOFWget.resume();
    }//GEN-LAST:event_resumeActionPerformed

    private void urlsParsedPropertyChange(java.beans.PropertyChangeEvent evt)//GEN-FIRST:event_urlsParsedPropertyChange
    {//GEN-HEADEREND:event_urlsParsedPropertyChange
        // TODO add your handling code here:
    }//GEN-LAST:event_urlsParsedPropertyChange

    private void urlsDownloadedPropertyChange(java.beans.PropertyChangeEvent evt)//GEN-FIRST:event_urlsDownloadedPropertyChange
    {//GEN-HEADEREND:event_urlsDownloadedPropertyChange
        // TODO add your handling code here:
    }//GEN-LAST:event_urlsDownloadedPropertyChange

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton exitButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jTextXXX;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JTextField localPathTxt;
    private javax.swing.JTextField maxBandwithTxt;
    private javax.swing.JTextField maxCpuTxt;
    private javax.swing.JTextField maxDownloaders;
    private javax.swing.JTextField maxLevel;
    private javax.swing.JTextField maxParsers;
    private javax.swing.JTextField numberDownloadersTxt;
    private javax.swing.JTextField numberParsersTxt;
    private javax.swing.JButton pauseButton;
    private javax.swing.JButton resumeButton;
    private javax.swing.JButton startButton;
    private javax.swing.JTextField urlTxt;
    private javax.swing.JTextPane urlsDownloaded;
    private javax.swing.JTextPane urlsParsed;
    // End of variables declaration//GEN-END:variables
    
    SystemObserver uu = SystemObserver.getInstance();
    
    private void startMonitorProcess ()
    {
        uu.setFields(numberDownloadersTxt, numberParsersTxt, urlsDownloaded, urlsParsed);
        uu.start();
    }
    
    private void stopMonitorProcess ()
    {
        uu.interrupt();
    }
}
